	/*the default options for jquery.form plugin */
	var options = {
		beforeSubmit:	ShowLoader,  	// pre-submit callback
		success:		ShowResponse,	// post-submit callback
		dataType:   	"json"			// 'xml', 'script', or 'json' (expected server response type)
	};
	/* end default options*/

	function ShowResponse(data, statusText, xhr, $form)
	{
		if(data.success){
           if(data.alert){
		   		alert(data.success);
		   }
		   else{
				$("#message").html('<div class="success">'+data.success+'</div>').show();
        	}
		}
		if(data.error){
			 if(data.alert){
			 	alert(data.error);
			 }
        	else{
				$("#message").html('<div class="error">'+data.error+'</div>').show();
        	}
		}
		if(data.reset){
			$form.resetForm();
		}
		if(data.redirect){
			window.location = data.redirect;
		}
		if(data.refresh){
			location.reload(true);
		}
		if(data.back){
			history.back();
		}
		if(data.list){
			$("#list:visible").html(data.list);
		}
		HideLoader();
	}

	function ShowLoader()
	{
		$("#message").hide();
		var h=$(window).height();
		$("#loader").css({height:h, opacity: 0.5});
		$("#loaderimg").css({margin: h/2});
		$("#loader").show();
	}
	function HideLoader()
	{
		$("#loader").hide();
	}

   /* Submit a form, ajax post method */
   function Send(id_form){

		if(Validate(id_form)){
       		$(id_form).ajaxSubmit(options);
		}
	   return false;
   }

   function Validate(id_form)
   {
   		return true;
        var m=0;
   		$(id_form).find("[rel]").each(function(index, Element){
   			var type=$(this).attr('rel');
			if(type=="m"){
				if($(this).val().trim()==""){
	                m++;
					$(this).addClass('err');
				}
				else{
	                $(this).removeClass('err');
				}
			}
			else if(type=="c"){  //must be checked

                if($(this).attr("checked")==false){
                    m++;
					$(this).addClass('err');
				}
				else{
	                $(this).removeClass('err');
				}
			}
   		});
		if(!m){
			return true;
		}
   		return false;
   }

   /* sort tables by column*/
   function Sort(column)
	{ dir=$("#sort_dir").val();
	  col=$("#sort_col").val();
	  if(col==column)
	  { if(dir=="asc")
	  		dir="desc";
	    else
			dir="asc";
      }
      $("#sort_col").val(column);
	  $("#sort_dir").val(dir);
	  $("#filter_form").ajaxSubmit(options);
	}

	function ToggleActive(data, elm)
	{
		if(data==1){
			$(elm).toggleClass(function(){
				var cl = $(elm).attr('class');
				$(elm).attr('class', '');
                if(cl=='activate')
					return 'deactivate';
				else
					return 'activate';
			});
		}
	}

	$(document).ready(function(){
		
		//add schortcuts
	   /*	$(document).bind('keydown', function(event) {
			if(event.keyCode==115)
			{					
				var href= $('#add').attr('href');
				if(href)
				{
					document.location =href;
				}
			}	
		});   */
		
		//set focus to first input element
		$('form input:text').eq(0).focus();
	});

	
	






